﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WarehouseWebAPP.code.BLL;

namespace WarehouseWebAPP.Pages.user.Edit_Entities
{
    public partial class EditDelivery : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void ComponentComboBox_DataBound(object sender, EventArgs e)
        {
            ComponentComboBox.Items.Insert(0, new ListItem("--ALL--", "%"));
        }
        protected void UseComboBox2_DataBound(object sender, EventArgs e)
        {
            UseComboBox2.Items.Insert(0, new ListItem("--ALL--", "%"));
        }

        protected void CustomerCComboBox_DataBound(object sender, EventArgs e)
        {
            CustomerCComboBox.Items.Insert(0, new ListItem("--ALL--", "%"));
        }


        protected void DeliveredDetailsView_DataBound(object sender, EventArgs e)
        {
            Label user = (Label)DeliveredDetailsView.FindControl("UserLabel");
            Label customer = (Label)DeliveredDetailsView.FindControl("CustomerLabel");
            Label component = (Label)DeliveredDetailsView.FindControl("ComponentLabel");
            if (user != null && customer != null && component != null)
            {
                UserBLL userBLL = new UserBLL();
                CustomerBLL customerBLL = new CustomerBLL();
                ComponentBLL componentBLL = new ComponentBLL();


                try
                {
                    user.Text = userBLL.userGetByNum(new Guid(user.Text))[0].user_name;
                    customer.Text = customerBLL.CustomerGetByNum(new Guid(customer.Text))[0].customer_name;
                    component.Text = componentBLL.ComponentGetByNum(new Guid(component.Text))[0].component_serial_num;
                }
                finally
                {
                    //TODO
                    //we should add dispose here
                    // we need to add dispose function foreach bll
                    // or maybe the detailsview disposes alone ,
                    // need to check, because there exists disposing event

                }

            }
        }

        protected void DeliveredDetailsView_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            MailPanel1.Visible = false;
            SentLabel.Visible = false;
            if (e.CommandName == "MailDelivery")
            {

                CustomerRadioButton.Enabled = true;
                UserRadioButton.Enabled = true;
                UserComboBox.Enabled = true;
                SentLabel.Visible = false;
                NotesTextBox.Text = "";
                NotesTextBox.Enabled = true;
                SendButton.Enabled = true;
                if (MailPanel1.Visible == false)
                {
                    MailPanel1.Visible = true;
                    SendButton.Focus();
                }
                else MailPanel1.Visible = false;
                DeliveredDetailsView.ChangeMode(DetailsViewMode.ReadOnly);

            }

        }

        protected void SendButton_Click(object sender, EventArgs e)
        {
            try
            {
                SentLabel.Focus();
                DeliveredBLL DeliveredBLL = new DeliveredBLL();
                CustomerBLL CustomerBLL = new CustomerBLL();
                UserBLL UserBLL = new UserBLL();
                ComponentBLL ComponentBLL = new ComponentBLL();
                string EmailAddress;
                string Subject = "Entity Details have been sent to you by The WareHouse";
                Guid Key = new Guid(DeliveredDetailsView.DataKey.Value.ToString());
                Guid ComponentKey = DeliveredBLL.DeliveredGetByNum(Key)[0].delivered_component;
                Guid UserKey = DeliveredBLL.DeliveredGetByNum(Key)[0].delivered_user;
                Guid CustomerKey = DeliveredBLL.DeliveredGetByNum(Key)[0].delivered_customer;
                DateTime DeliveredDate = DeliveredBLL.DeliveredGetByNum(Key)[0].delivered_date;
                
                String ComponentSerial = ComponentBLL .ComponentGetByNum(ComponentKey)[0].component_serial_num;
                String UserName = UserBLL.userGetByNum(UserKey)[0].user_name;
                String CustomerName = CustomerBLL.CustomerGetByNum(CustomerKey)[0].customer_name;
                String ComponentQuantity = DeliveredBLL.DeliveredGetByNum(Key)[0].delivered_quantity.ToString();

                String[] fields = { "Component Serial", "Delivery Date", "Customer Name", "Delivering User", "Component Quantity" };
                String[] values = { ComponentSerial, DeliveredDate.ToShortDateString(), CustomerName, UserName, ComponentQuantity };

                String EntityString = Mail.HTMLEntityDecorator("Delivery Details", fields, values);
                if (CustomerRadioButton.Checked)
                {
                    string CustomerMail = CustomerBLL.CustomerGetByNum(CustomerKey)[0].customer_email;
                    EmailAddress = CustomerMail;

                }
                else
                {

                    string UserMail = UserBLL.userGetByNum(new Guid(UserComboBox.SelectedValue))[0].user_email;
                    EmailAddress = UserMail;
                }

                Boolean res = Mail.SendMail(EmailAddress, Subject, Mail.SenderNotes(NotesTextBox.Text) + EntityString);
                if (res == false)
                {
                    SentLabel.Text = "Mail Failed";
                }
            }

            catch (Exception)
            {
                SentLabel.Text = "Mail Failed";

            }

            finally
            {
                //TODO:Dispose;
                CustomerRadioButton.Enabled = false;
                UserRadioButton.Enabled = false;
                NotesTextBox.Enabled = false;
                SendButton.Enabled = false;
                UserComboBox.Enabled = false;
                SentLabel.Visible = true;
                MailPanel1.Visible = false;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SentLabel.Visible = false;
            MailPanel1.Visible = false;
        }




    }
}
